﻿using System;
using System.Collections.Generic;
using System.Linq;
using DomainModel;
using NLog;
using Services.Abstract;

namespace Web
{
    public class Global : System.Web.HttpApplication
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected void Application_Start(object sender, EventArgs e)
        {
            logger.Info("Application started.");

            IoC.StructureMap.Configure();

            IUserService userService = IoC.StructureMap.Resolve<IUserService>();
            IUserProfileService userProfileService = IoC.StructureMap.Resolve<IUserProfileService>();

            if (!userService.LoginIsBusy("admin"))
            {
                User admin = new User("admin", "admin email", Role.Admin) { Approved = true };
                admin.SetPasswordHash("admin");
                admin.UserProfile = new UserProfile("admin fio") { User = admin };

                userService.SaveOrUpdate(admin);

                //                FillTestDatabase();
                FillTestDB();
            }

            IScheduler scheduler = IoC.StructureMap.Resolve<IScheduler>();
            scheduler.Start();
        }

        private void FillTestDB()
        {
            IStudyYearService studyYearService = IoC.StructureMap.Resolve<IStudyYearService>();
            ISemesterService semesterService = IoC.StructureMap.Resolve<ISemesterService>();
            ISubjectService subjectService = IoC.StructureMap.Resolve<ISubjectService>();
            IGroupService groupService = IoC.StructureMap.Resolve<IGroupService>();
            IUserService userService = IoC.StructureMap.Resolve<IUserService>();
            ITimetableUnitService timetableUnitService = IoC.StructureMap.Resolve<ITimetableUnitService>();
            IRingService ringService = IoC.StructureMap.Resolve<IRingService>();
            IDateTimeService dateTimeService = IoC.StructureMap.Resolve<IDateTimeService>();
            ILessonService lessonService = IoC.StructureMap.Resolve<ILessonService>();

            #region Rings

            var startDateTime = new DateTime(1, 1, 1, 8, 0, 0);
            for (int i = 1; i <= 7; i++)
            {
                var endDateTime = startDateTime.AddHours(1).AddMinutes(20);

                var ring = new Ring(i, startDateTime, endDateTime);
                ringService.SaveOrUpdate(ring);

                startDateTime = endDateTime;
                startDateTime = i % 2 == 1 ? startDateTime.AddMinutes(15) : startDateTime.AddMinutes(20);
            }

            #endregion

            #region StudyYear And Semesters

            var studyYear = new StudyYear(new DateTime(2012, 9, 1), new DateTime(2013, 5, 31));
            studyYearService.SaveOrUpdate(studyYear);

            var semester = new StudySemester(studyYear, new DateTime(2012, 9, 1), new DateTime(2012, 12, 31));
            semesterService.SaveOrUpdate(semester);

            semester = new StudySemester(studyYear, new DateTime(2013, 1, 1), new DateTime(2013, 5, 31));
            semesterService.SaveOrUpdate(semester);

            #endregion

            #region Subjects

            var subjectNames = new Dictionary<string, string>
                                   {
                                       {"Технологии проектирования программных систем", "ТППС"},
                                       {"Охрана труда", "Охр.труда"},
                                       {"Проектирование интерфейсов пользователя", "Пр.интерф."},
                                       {"Интеллектуальная собственность", "Инт.собст."},
                                       {"Проектирование компьютерных систем и сетей", "ПКСМ"},
                                       {"Программирование компьютерных сетей", "Пр.КМ"},
                                       {"Сетевые информационные технологии", "МИТ"},
                                       {"Компьютерные системы искуственного интеллекта", "Иск.инт."},
                                       {"Проектирование встроенных компьютерных систем", "Пр.встр.КС"},
                                       {"Метрология", ""},
                                       {"Проектирование специализированных компьютерных систем", "Пр.СКС"},
                                       {"Программное обеспечение специализированных компьютерных систем", "Пр.обесп.СКС"},
                                   };

            foreach (var subjectName in subjectNames)
            {
                var subject = new Subject(subjectName.Key, subjectName.Value);
                subjectService.SaveOrUpdate(subject);
            }

            #endregion

            #region Groups

            var groupNames = new List<string>
                                 {
                                     "КС-081",
                                     "КС-082",
                                     "СП-081",
                                     "СК-081",
                                 };

            foreach (var groupName in groupNames)
            {
                var group = new Group(groupName);
                groupService.SaveOrUpdate(group);
            }

            #endregion

            #region Teachers

            var teacherLogins = new Dictionary<string, string>
                                    {
                                        {"prila", "Прелая О.А."},
                                        {"nikitenko", "Никитенко Е.В."},
                                        {"hizg", "Хижняк А.В."},
                                        {"nester", "Нестеренко С.А."},
                                        {"ryndych", "Риндич Е.В."},
                                        {"balchenko", "Бальченко И.В."},
                                        {"trunova", "Трунова И.В."},
                                        {"bogdan", "Богдан И.В."},
                                        {"harchenko", "Харченко М.В."},
                                        {"stasuk", "Стасюк С.С."},
                                        {"torgon", "Торгонский В.В."},
                                        {"rogovenko", "Роговенко А.И."},
                                    };

            foreach (var teacherLogin in teacherLogins)
            {
                User teacher = new User(teacherLogin.Key, teacherLogin.Key + "@gmail.com", Role.Teacher) { Approved = true };
                teacher.SetPasswordHash("1");
                teacher.UserProfile = new UserProfile(teacherLogin.Value) { User = teacher };
                userService.SaveOrUpdate(teacher);
            }

            #endregion

            #region Students

            var groups = groupService.GetAll().OrderBy(g => g.Id);
            int k = 1;
            foreach (var group in groups)
            {
                for (int i = 1; i <= 10; i++)
                {
                    var student = new User("student" + k, "student" + k + "@gmail.com", Role.Student) { Approved = true, Group = group };
                    student.SetPasswordHash("1");
                    student.UserProfile = new UserProfile("Студент " + k) { User = student };
                    userService.SaveOrUpdate(student);
                    k++;
                }
            }

            #endregion

            #region TimetableUnits

            #region Subjects

            var subjects = subjectService.GetAll().OrderBy(s => s.Name);
            var tpps = subjects.Single(s => s.Name == subjectNames.Keys.ElementAt(0));
            var ohrTr = subjects.Single(s => s.Name == subjectNames.Keys.ElementAt(1));
            var prInterf = subjects.Single(s => s.Name == subjectNames.Keys.ElementAt(2));
            var intSobst = subjects.Single(s => s.Name == subjectNames.Keys.ElementAt(3));
            var pksm = subjects.Single(s => s.Name == subjectNames.Keys.ElementAt(4));
            var prKM = subjects.Single(s => s.Name == subjectNames.Keys.ElementAt(5));
            var mit = subjects.Single(s => s.Name == subjectNames.Keys.ElementAt(6));
            var iskInt = subjects.Single(s => s.Name == subjectNames.Keys.ElementAt(7));
            var prVstrKS = subjects.Single(s => s.Name == subjectNames.Keys.ElementAt(8));
            var metrolog = subjects.Single(s => s.Name == subjectNames.Keys.ElementAt(9));
            var prSKS = subjects.Single(s => s.Name == subjectNames.Keys.ElementAt(10));
            var prObespSKS = subjects.Single(s => s.Name == subjectNames.Keys.ElementAt(11));

            #endregion

            #region Groups

            groups = groupService.GetAll().OrderBy(g => g.Id);
            var KS81 = groups.Single(g => g.Name == groupNames[0]);
            var KS82 = groups.Single(g => g.Name == groupNames[1]);
            var SP81 = groups.Single(g => g.Name == groupNames[2]);
            var SK81 = groups.Single(g => g.Name == groupNames[3]);

            #endregion

            #region Teachers

            var teachers = userService.GetUsers(Role.Teacher);
            var prila = teachers.Single(s => s.Login == teacherLogins.Keys.ElementAt(0));
            var nikitenko = teachers.Single(s => s.Login == teacherLogins.Keys.ElementAt(1));
            var hizg = teachers.Single(s => s.Login == teacherLogins.Keys.ElementAt(2));
            var nester = teachers.Single(s => s.Login == teacherLogins.Keys.ElementAt(3));
            var ryndych = teachers.Single(s => s.Login == teacherLogins.Keys.ElementAt(4));
            var balchenko = teachers.Single(s => s.Login == teacherLogins.Keys.ElementAt(5));
            var trunova = teachers.Single(s => s.Login == teacherLogins.Keys.ElementAt(6));
            var bogdan = teachers.Single(s => s.Login == teacherLogins.Keys.ElementAt(7));
            var harchenko = teachers.Single(s => s.Login == teacherLogins.Keys.ElementAt(8));
            var stasuk = teachers.Single(s => s.Login == teacherLogins.Keys.ElementAt(9));
            var torgon = teachers.Single(s => s.Login == teacherLogins.Keys.ElementAt(10));
            var rogovenko = teachers.Single(s => s.Login == teacherLogins.Keys.ElementAt(11));

            #endregion

            var semesters = semesterService.GetAll().OrderBy(s => s.BeginDate);
            foreach (var studySemester in semesters)
            {
                #region week1

                #region monday

                #region pair2

                var timetableUnit = new TimetableUnit(studySemester, 1, 1, 2, KS81, tpps, prila, WorkType.Lection, "210");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 1, 2, KS82, tpps, prila, WorkType.Lection, "210");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 1, 2, SP81, tpps, prila, WorkType.Lection, "210");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair3

                timetableUnit = new TimetableUnit(studySemester, 1, 1, 3, KS81, ohrTr, nikitenko, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 1, 3, KS82, ohrTr, nikitenko, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 1, 3, SP81, ohrTr, nikitenko, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 1, 3, SK81, ohrTr, nikitenko, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair4

                timetableUnit = new TimetableUnit(studySemester, 1, 1, 4, KS81, prInterf, hizg, WorkType.Lab, "73");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 1, 4, KS82, pksm, ryndych, WorkType.Lab, "51");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 1, 4, SK81, prVstrKS, nikitenko, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #endregion

                #region tuesday

                #region pair1

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 1, KS81, intSobst, nester, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 1, KS82, intSobst, nester, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 1, SP81, intSobst, nester, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 1, SK81, intSobst, nester, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair2

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 2, KS81, pksm, ryndych, WorkType.Lab, "51");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 2, SP81, iskInt, bogdan, WorkType.Lab, "65");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 2, SK81, metrolog, nikitenko, WorkType.Lection, "93");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair3

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 3, KS81, prKM, hizg, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 3, KS82, prKM, hizg, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 3, SP81, prKM, hizg, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 3, SK81, pksm, ryndych, WorkType.Lab, "72");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair4

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 4, SP81, tpps, prila, WorkType.Lab, "65");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 2, 4, SK81, pksm, ryndych, WorkType.Lab, "72");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #endregion

                #region wednesday

                #region pair1

                timetableUnit = new TimetableUnit(studySemester, 1, 3, 1, KS82, pksm, ryndych, WorkType.Lab, "54");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 3, 1, SP81, prKM, prila, WorkType.Lab, "73");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair2

                timetableUnit = new TimetableUnit(studySemester, 1, 3, 2, KS81, pksm, ryndych, WorkType.Lection, "309");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 3, 2, KS82, pksm, ryndych, WorkType.Lection, "309");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 3, 2, SP81, pksm, ryndych, WorkType.Lection, "309");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 3, 2, SK81, pksm, ryndych, WorkType.Lection, "309");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair3

                timetableUnit = new TimetableUnit(studySemester, 1, 3, 3, KS82, iskInt, bogdan, WorkType.Lab, "72");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 3, 3, SP81, prInterf, prila, WorkType.Lab, "54");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 3, 3, SK81, prSKS, torgon, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair4

                timetableUnit = new TimetableUnit(studySemester, 1, 3, 4, SK81, prSKS, rogovenko, WorkType.Lab, "92");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #endregion

                #region tuesday

                #region pair1

                timetableUnit = new TimetableUnit(studySemester, 1, 4, 1, KS81, mit, balchenko, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 4, 1, KS82, mit, balchenko, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 4, 1, SP81, mit, balchenko, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair2

                timetableUnit = new TimetableUnit(studySemester, 1, 4, 2, KS81, iskInt, trunova, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 4, 2, KS82, iskInt, trunova, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 4, 2, SP81, iskInt, trunova, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 4, 2, SK81, iskInt, trunova, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair3

                timetableUnit = new TimetableUnit(studySemester, 1, 4, 3, KS82, tpps, prila, WorkType.Lab, "65");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 4, 3, SK81, prSKS, rogovenko, WorkType.Lab, "93");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair4

                timetableUnit = new TimetableUnit(studySemester, 1, 4, 4, KS82, prInterf, prila, WorkType.Lab, "64");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #endregion

                #region friday

                #region pair1

                timetableUnit = new TimetableUnit(studySemester, 1, 5, 1, KS81, tpps, prila, WorkType.Lab, "73");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair2

                timetableUnit = new TimetableUnit(studySemester, 1, 5, 2, KS81, prInterf, hizg, WorkType.Lab, "73");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 5, 2, KS82, mit, balchenko, WorkType.Lab, "72");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 5, 2, SK81, prVstrKS, nikitenko, WorkType.Lab, "93");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair3

                timetableUnit = new TimetableUnit(studySemester, 1, 5, 3, KS81, prInterf, hizg, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 5, 3, KS82, prInterf, hizg, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 5, 3, SP81, prInterf, hizg, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 5, 3, SK81, prObespSKS, stasuk, WorkType.Lection, "93");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair4

                timetableUnit = new TimetableUnit(studySemester, 1, 5, 4, KS82, tpps, prila, WorkType.Lab, "51");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 5, 4, SK81, metrolog, nikitenko, WorkType.Lab, "93");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair5

                timetableUnit = new TimetableUnit(studySemester, 1, 5, 5, KS82, mit, balchenko, WorkType.Lab, "72");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 1, 5, 5, SK81, iskInt, bogdan, WorkType.Lab, "54");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #endregion

                #endregion

                #region week2

                #region monday

                #region pair1

                timetableUnit = new TimetableUnit(studySemester, 2, 1, 1, KS81, iskInt, trunova, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 1, 1, KS82, iskInt, trunova, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 1, 1, SP81, iskInt, trunova, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 1, 1, SK81, iskInt, trunova, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair2

                timetableUnit = new TimetableUnit(studySemester, 2, 1, 2, KS82, iskInt, bogdan, WorkType.Lab, "65");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 1, 2, SP81, pksm, stasuk, WorkType.Lab, "54");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 1, 2, SK81, prSKS, torgon, WorkType.Lection, "93");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair3

                timetableUnit = new TimetableUnit(studySemester, 2, 1, 3, SP81, prKM, prila, WorkType.Lab, "73");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 1, 3, SK81, prVstrKS, rogovenko, WorkType.Lab, "93");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #endregion

                #region tuesday

                #region pair1

                timetableUnit = new TimetableUnit(studySemester, 2, 2, 1, KS81, tpps, prila, WorkType.Lab, "76");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 2, 1, SK81, pksm, ryndych, WorkType.Lab, "51");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair2

                timetableUnit = new TimetableUnit(studySemester, 2, 2, 2, KS81, prKM, hizg, WorkType.Lab, "73");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 2, 2, KS82, prInterf, prila, WorkType.Lab, "54");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 2, 2, SP81, iskInt, bogdan, WorkType.Lab, "65");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 2, 2, SK81, prObespSKS, stasuk, WorkType.Lection, "93");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair3

                timetableUnit = new TimetableUnit(studySemester, 2, 2, 3, KS81, tpps, prila, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 2, 3, KS82, tpps, prila, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 2, 3, SP81, tpps, prila, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair4

                timetableUnit = new TimetableUnit(studySemester, 2, 2, 4, KS82, tpps, prila, WorkType.Lection, "64");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #endregion

                #region wednesday

                #region pair1

                timetableUnit = new TimetableUnit(studySemester, 2, 3, 1, KS81, pksm, ryndych, WorkType.Lab, "51");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 3, 1, KS82, tpps, prila, WorkType.Lab, "76");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair2

                timetableUnit = new TimetableUnit(studySemester, 2, 3, 2, KS81, pksm, ryndych, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 3, 2, KS82, pksm, ryndych, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 3, 2, SP81, pksm, ryndych, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 3, 2, SK81, pksm, ryndych, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair3

                timetableUnit = new TimetableUnit(studySemester, 2, 3, 3, KS81, prKM, hizg, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 3, 3, KS82, prKM, hizg, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 3, 3, SP81, prKM, hizg, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 3, 3, SK81, metrolog, nikitenko, WorkType.Lab, "93");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #endregion

                #region tuesday

                #region pair1

                timetableUnit = new TimetableUnit(studySemester, 2, 4, 1, KS81, pksm, ryndych, WorkType.Lab, "51");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 4, 1, SK81, prVstrKS, nikitenko, WorkType.Lection, "93");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair2

                timetableUnit = new TimetableUnit(studySemester, 2, 4, 2, KS81, prInterf, hizg, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 4, 2, KS82, prInterf, hizg, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 4, 2, SP81, prInterf, hizg, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 4, 2, SK81, prVstrKS, nikitenko, WorkType.Lab, "93");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair3

                timetableUnit = new TimetableUnit(studySemester, 2, 4, 3, KS81, mit, balchenko, WorkType.Lab, "72");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 4, 3, KS82, prKM, prila, WorkType.Lab, "65");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #endregion

                #region friday

                #region pair1

                timetableUnit = new TimetableUnit(studySemester, 2, 5, 1, KS81, tpps, prila, WorkType.Lab, "76");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair2

                timetableUnit = new TimetableUnit(studySemester, 2, 5, 2, KS81, mit, balchenko, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 5, 2, KS82, mit, balchenko, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 5, 2, SP81, mit, balchenko, WorkType.Lection, "211");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 5, 2, SK81, metrolog, nikitenko, WorkType.Lection, "93");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair3

                timetableUnit = new TimetableUnit(studySemester, 2, 5, 3, KS81, tpps, prila, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 5, 3, KS82, tpps, prila, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 5, 3, SP81, tpps, prila, WorkType.Lection, "95");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 5, 3, SK81, prSKS, rogovenko, WorkType.Lab, "92");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #region pair4

                timetableUnit = new TimetableUnit(studySemester, 2, 5, 4, KS81, iskInt, trunova, WorkType.Lab, "54");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                timetableUnit = new TimetableUnit(studySemester, 2, 5, 4, SP81, mit, harchenko, WorkType.Lab, "72");
                timetableUnitService.SaveOrUpdate(timetableUnit);

                #endregion

                #endregion

                #endregion
            }

            #endregion

            #region Lessons

            var marks = new List<string> { "5", "4", "3", "2", "1", "Н", "" };

            Random rnd = new Random();

            var currentDate = studyYear.BeginDate;

            while (currentDate < DateTime.Now)
            {
                semester = semesterService.GetSemesterByDate(currentDate);
                var weekNo = dateTimeService.GetWeekNoByCurrentDay(semester, currentDate);
                var dayNo = dateTimeService.GetDayNoByCurrentDay(currentDate);

                for (int pairNo = 1; pairNo <= 7; pairNo++)
                {
                    var ring = ringService.GetRingByPairNo(pairNo);

                    var timetableUnits = timetableUnitService.GetTimetableUnits(semester, weekNo, dayNo, pairNo);

                    foreach (var timetableUnit in timetableUnits)
                    {
                        var students = userService.GetUsers(timetableUnit.Group);
                        foreach (var student in students)
                        {
                            var lessonDateTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day,
                                                              ring.BeginTime.Hour, ring.BeginTime.Minute, 0);

                            var lesson = new Lesson(lessonDateTime, student, timetableUnit);

                            int markNo = rnd.Next(1, 8) - 1;
                            lesson.Mark = marks[markNo];

                            lessonService.SaveOrUpdate(lesson);
                        }
                    }
                }

                currentDate = currentDate.AddDays(1);
            }

            #endregion

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}